﻿using System;
using System.Collections.Generic;
using System.Web;

using umbraco;
using umbraco.BusinessLogic;
using umbraco.BusinessLogic.console;
using umbraco.cms.businesslogic.web;
#if UMB45
using umbraco.presentation.nodeFactory;
#else
using umbraco.NodeFactory;
#endif

namespace umbraco.blog4 {

    public class BlogDateFolder : ApplicationBase {
        /// <summary>
        /// Initializes a new instance of the <see cref="BlogDateFolder"/> class.
        /// </summary>
        public BlogDateFolder() {
            // Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);          
        }

        /// <summary>
        /// Moves a Post if the Post Date Changes
        /// </summary>
        /// <param name="sender">Document Being Published</param>
        /// <param name="e">Publish Event Args</param>
        /*
        void Document_BeforePublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e) {            
            if (BlogLibrary.IsType(Post.DocumentTypeName, sender.Id)) {
                Post p = new Post(sender.Id);
                Log.Add(LogTypes.Debug, sender.User, sender.Id, string.Format("Start Before Publish Event for {0} {1}", sender.ContentType.Alias, sender.Id));

                // We're moving it if:
                // 1) Datefolders are enabled/formatted right
                // 2) Post date has changed, or is a brand-new post.

                try {
                    DocumentVersionList[] postVersions = sender.GetVersions();
                    bool _versionCheck = true;
                    DateTime postDate;
                    postDate = System.Convert.ToDateTime(sender.getProperty("postDate").Value.ToString());



                    //If it has been published, check post date info
                    if (postVersions.Length > 1) {
                        //Length -1 is current version Length -2 is past version (if it exists)
                        Guid previousVersion = postVersions[postVersions.Length - 2].Version;
                        Document doc = new Document(sender.Id, previousVersion);
                        DateTime previousPostDate = Convert.ToDateTime(doc.getProperty("postDate").Value.ToString());
                        _versionCheck = (postDate != previousPostDate);
                    }

                    Document d = new Document(sender.Parent.Id);

                    string[] postPath = d.getProperty("dateFolderFormat").Value.ToString().Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                    if (postPath.Length > 0) { //we have datefolders... ;-)
                                
                    }

                    bool useDateFolders = false;
                    if (d.getProperty("useDateFolders") != null) { useDateFolders = (int.Parse(d.getProperty("useDateFolders").Value.ToString()) == 1 ? true : false); }

                        if ((_versionCheck) || ((d.ContentType.Alias != Blog.DTDefault_DateFolder) && (useDateFolders))) {
                            string[] strArray = { postDate.Year.ToString(), postDate.Month.ToString(), postDate.Day.ToString() };
                            if (strArray.Length == 3) {
                                Node topBlogLevel = new Node(sender.Parent.Id);
                                //Traverse up the tree to Find the Blog Node since we are likely in a Date Folder path

                                while (topBlogLevel != null && !dtblog.Contains(DocumentType.GetByAlias(topBlogLevel.NodeTypeAlias))) {
                                    if (topBlogLevel.Parent != null) {
                                        topBlogLevel = new Node(topBlogLevel.Parent.Id);
                                    } else {
                                        topBlogLevel = null;
                                    }
                                }
                                if (topBlogLevel != null) {
                                    Document document = null;
                                    Node folderNode = null;
                                    foreach (Node ni in topBlogLevel.Children) {
                                        if (ni.Name == strArray[0]) {
                                            folderNode = new Node(ni.Id);
                                            document = new Document(ni.Id);
                                            break;
                                        }
                                    }
                                    if (folderNode == null) {
                                        document = Document.MakeNew(strArray[0], DocumentType.GetByAlias(Blog.DTDefault_DateFolder), sender.User, topBlogLevel.Id);
                                        document.Publish(sender.User);
                                        library.UpdateDocumentCache(document.Id);
                                        folderNode = new Node(document.Id);
                                    }
                                    Node folderNode2 = null;
                                    foreach (Node ni in folderNode.Children) {
                                        if (ni.Name == strArray[1]) {
                                            folderNode2 = new Node(ni.Id);
                                            break;
                                        }
                                    }
                                    if (folderNode2 == null) {
                                        Document document2 = Document.MakeNew(strArray[1], DocumentType.GetByAlias(Blog.DTDefault_DateFolder), sender.User, folderNode.Id);
                                        document2.Publish(sender.User);
                                        library.UpdateDocumentCache(document2.Id);
                                        folderNode2 = new Node(document2.Id);
                                    }
                                    Document document3 = null;  //As this is the last check, a document object is fine
                                    foreach (Node ni in folderNode2.Children) {
                                        if (ni.Name == strArray[2]) {
                                            document3 = new Document(ni.Id);
                                            break;
                                        }
                                    }
                                    if (document3 == null) {
                                        document3 = Document.MakeNew(strArray[2], DocumentType.GetByAlias(Blog.DTDefault_DateFolder), sender.User, folderNode2.Id);
                                        document3.Publish(sender.User);
                                        library.UpdateDocumentCache(document3.Id);
                                    }
                                    if (sender.Parent.Id != document3.Id) {
                                        sender.Move(document3.Id);
                                        Log.Add(LogTypes.Debug, sender.User, sender.Id, string.Format("Move Required for BlogPost {0} for PostDate {1}.  Moved Under Node {2}", sender.Id, postDate.ToShortDateString(), document3.Id));
                                    }
                                } else {
                                    Log.Add(LogTypes.Debug, sender.User, sender.Id, string.Format("Unable to determine top of Blog for BlogPost {0} while attempting to move to new Post Date", sender.Id));
                                }

                            }
                        }
                    } catch (Exception Exp) { Log.Add(LogTypes.Debug, sender.User, sender.Id, string.Format("Error while Finding Blog Folders for BlogPost {0} while trying to move to new Post Date.  Error: {1}", sender.Id, Exp.Message)); }
                    umbraco.library.RefreshContent();
                }
            }
         */
    }
}
